package com.hsh.maoduo.common;

import com.hsh.maoduo.constant.common.ResultConstants;
import lombok.Data;

@Data
public class Result<T> {
    private boolean success;

    private String code;

    private String msg;

    private T data;

    public static Result success() {
        Result result = new Result();
        result.success = true;
        result.code = ResultConstants.CODE_SUCCESS;
        result.msg = ResultConstants.MSG_SUCCESS;
        return result;
    }

    public static Result create(int successNum) {
        if (successNum == 0) {
            return fail(successNum);
        } else {
            return success(successNum);
        }
    }

    public static Result success(String msg) {
        Result result = new Result();
        result.success = true;
        result.code = ResultConstants.CODE_SUCCESS;
        result.msg = msg;
        result.data = msg;
        return result;
    }

    public  static <T> Result<T> success(T data) {
        Result result = new Result();
        result.success = true;
        result.code = ResultConstants.CODE_SUCCESS;
        result.msg = ResultConstants.MSG_SUCCESS;
        result.data = data;
        return result;
    }

    public  static <T> Result<T> success(String msg,T data) {
        Result result = new Result();
        result.success = true;
        result.code = ResultConstants.CODE_SUCCESS;
        result.msg = msg;
        result.data = data;
        return result;
    }

    public static Result fail() {
        Result result = new Result();
        result.success = false;
        result.code = ResultConstants.CODE_FAIL;
        result.msg = ResultConstants.MSG_FAIL;
        return result;
    }

    public static <T> Result<T> fail(T data) {
        Result result = new Result();
        result.success = false;
        result.code = ResultConstants.CODE_FAIL;
        result.msg = ResultConstants.MSG_FAIL;
        result.data = data;
        return result;
    }
}
